package com.kuaidian.controller;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kuaidian.model.api.ResponseMsg;
import com.kuaidian.pojo.Row;
import com.kuaidian.pojo.TCollect;
import com.kuaidian.service.CollectService;
import com.kuaidian.service.RedisService;
import com.kuaidian.service.UserService;
import com.kuaidian.util.CommonUtil;
import com.kuaidian.util.JacksonMgr;
import com.kuaidian.util.constant.Constant;
import com.kuaidian.util.constant.ErrorCode;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;


@Api(value="Common",description="user")
@Controller
@RequestMapping( "/api/user" )
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private CollectService collectService;
    @Autowired
    private RedisService redisService;
    
    private static Logger logger = Logger.getLogger(UserController.class);  
    
    @InitBinder     /* Converts empty strings into null when a form is submitted */
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
    }
    
	@ApiOperation(value ="收藏/取消收藏",notes="收藏/取消收藏")
    @RequestMapping(value="collect",produces="application/json;charset=utf-8",method = RequestMethod.GET)
    public @ResponseBody Object collect(@RequestParam Integer  user_id,
    										  @RequestParam Integer  photo_id,
    										  @RequestParam String  c_type){
    	try {
    		ResponseMsg retMsg = null;
    		if(Constant.TYPE_1.equals(c_type)){//收藏
    			TCollect collect = new TCollect();
    			collect.setUser_id(user_id);
    			collect.setPhoto_id(photo_id);
    			collect.setCreate_time(new Date());
    			collectService.insertCollect(collect);
    			retMsg = new ResponseMsg("收藏成功",ErrorCode.OK, null);
    		}else if(Constant.TYPE_2.equals(c_type)){//取消收藏
    			TCollect collect = new TCollect();
    			collect.setUser_id(user_id);
    			collect.setPhoto_id(photo_id);
    			collectService.deleteCollect(collect);
    			retMsg = new ResponseMsg("取消收藏成功",ErrorCode.OK, null);
    		}else{
    			retMsg = new ResponseMsg("收藏类型错误",ErrorCode.ERROR, null);
    		}
        	String jsonString = JacksonMgr.toJSON(retMsg);
    		return jsonString;
		} catch (Exception e) {
			logger.error(e);
			return JacksonMgr.makeExceptionJSON(e);
		}

    }
	
	@ApiOperation(value ="查询我的收藏",notes="查询我的收藏")
    @RequestMapping(value="selectMyCollection",produces="application/json;charset=utf-8",method = RequestMethod.GET)
    public @ResponseBody Object selectMyCollection(@RequestParam Integer  user_id,
    											   @RequestParam Integer pageSize, 
    											   @RequestParam Integer curPage){
    	try {
    		ResponseMsg retMsg = null;
    		TCollect collect_p = new TCollect();
    		collect_p.setUser_id(user_id);
    		Integer startRow = CommonUtil.curPage2Start(pageSize, curPage);
    		List<Row> collect_r = collectService.selectCollection(collect_p, startRow, pageSize);
    		retMsg = new ResponseMsg("查询成功",ErrorCode.OK, collect_r);
        	String jsonString = JacksonMgr.toJSON(retMsg);
    		return jsonString;
		} catch (Exception e) {
			logger.error(e);
			return JacksonMgr.makeExceptionJSON(e);
		}

    }
	
	@ApiOperation(value ="登出",notes="登出")
    @RequestMapping(value="logout",produces="application/json;charset=utf-8",method = RequestMethod.GET)
    public @ResponseBody Object logout(@RequestParam Integer  user_id){
    	try {
    		ResponseMsg retMsg = null;
    		redisService.delCachedLoginedUser(user_id);
    		retMsg = new ResponseMsg("查询成功",ErrorCode.OK, null);
        	String jsonString = JacksonMgr.toJSON(retMsg);
    		return jsonString;
		} catch (Exception e) {
			logger.error(e);
			return JacksonMgr.makeExceptionJSON(e);
		}

    }
	 
	
	 

 	 
}
